<?php
/**
 * AssertEmpty.php
 * 
 * @category   Naneau
 * @package    Naneau_Action_Helper
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.2
 * @todo MAJOR OVERHAUL
 */

/** base class */
require_once 'Zend/Controller/Action/Helper/Abstract.php';

/**
 * Naneau_Controller_Action_Helper_AssertEmpty
 * 
 * raise 404 on conditions
 *
 * @category   Naneau
 * @package    Naneau_Action_Helper
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Action_Helper_AssertEmpty extends Zend_Controller_Action_Helper_Abstract {

    /**
     * module to redirect to
     *
     * @var string
     */
    private $_module = 'default';

    /**
     * controller to redirect to
     *
     * @var string
     */
    private $_controller = 'error';

    /**
     * action to redirect to
     *
     * @var unknown_type
     */
    private $_action = 'error';

    /**
     * additional parameters to pass to the redirect
     *
     * @var array
     */
    private $_params = array();

    /**
     * always throw an exception?
     *
     * @var bool
     */
    private $_throwExceptionAlways = true;

    /**
     * constructor
     * tries to set some sensible defaults
     */
    public function __construct() {
        $front = Zend_Controller_Front::getInstance();
        //front controller
        $errorHandler = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');
        //the error handler plugin
        if ($errorHandler) {
            $this->_module = $errorHandler->getErrorHandlerModule();
            $this->_controller = $errorHandler->getErrorHandlerController();
            $this->_action = $errorHandler->getErrorHandlerAction();
            //update defaults with the ones set in error handler
        }
    }

    /**
     * always throw an exception?
     *
     * @param bool $flag
     */
    public function throwExceptionAlways($flag = true) {
        $this->_throwExceptionAlways = $flag;
        return $this;
    }

    /**
     * set the module for the redirect
     *
     * @param string $module
     */
    public function setModule($module) {
        $this->_module = $module;
        return $this;
    }

    /**
     * get the module used for the redirect
     *
     * @return string
     */
    public function getModule() {
        return $this->_module;
    }

    /**
     * set the controller for the redirect
     *
     * @param string $controller
     */
    public function setController($controller) {
        $this->_controller = $controller;
        return $this;
    }

    /**
     * get the controller used for the redirect
     *
     * @return string
     */
    public function getController() {
        return $this->_controller;
    }

    /**
     * set the action for the redirect
     *
     * @param string $action
     */
    public function setAction($action) {
        $this->_action = $action;
        return $this;
    }

    /**
     * get the action used for the redirect
     *
     * @return string
     */
    public function getAction() {
        return $this->_action;
    }

    /**
     * add a param to the redirect
     *
     * @param string $key
     * @param mixed $value
     */
    public function addParam($key, $value) {
        $this->_params[$key] = $value;
        return $this;
    }

    /**
     * get the params
     *
     * @return array
     */
    public function getParams() {
        return $this->_params;
    }

    /**
     * convenience method, for direct access
     */
    public function direct() {
        $args = func_get_args();
        return call_user_func_array(array($this, 'check'), $args);
    }

    /**
     * redirect if:
     * 
     * - empty in case of string (trim() applied first)
     * - null/false
     *
     * @param mixed $what
     * @param bool $exception
     * @todo implement more robust checks for certain object types
     */
    public function check($what, $exception = true) {
        if (is_string($what)) {
            $what = trim($what);
        }
        //trim strings

        $check = true;
        //let's start optimistically

        if ($what instanceof Zend_Db_Table_Rowset) {
            //meh :x
            $check = ($what->count() > 0);
        }
        elseif (empty($what) || $what === false) {
            $check = false;
        }

        if (!$check) {
            if ($exception || $this->_throwExceptionAlways = true) {
                //we've been asked to throw an exception
                require_once 'Naneau/Exception/Empty.php';
                throw new Naneau_Exception_Empty('Page can\'t be found');
                //so let's throw it!
            }
            return false;
            //if no exception should be thrown, return false
        }

        return $what;
        //return if no check doesn't fail
    }

    private function blah() {
        //WISH THIS WOULD WORK

        $request = $this->getRequest();
        //the request
        $request->setModuleName($this->_module);
        $request->setControllerName($this->_controller);
        $request->setActionName($this->_action);
        $request->setParams($this->_params);
        //set new module/controller/action/params
        $request->setDispatched(false);
        //reset dispatched

        //THIS DOES WORK, (but is a redirect)

        $redirector = $this->getActionController()->getHelper('Redirector');
        //redirector action helper (why not reuse?)
        if ($redirector === null) {
            //check if the helper exists
            throw new Exception('Redirector action helper not found');
        }

        $redirector->setExit(false);
        $redirector->goto($this->_action, $this->_controller, $this->_module, $this->_params);
        //redirect (oh, and exit!)
    }
}